// First app example using Riverpod annotations (code generation)

import 'package:flutter_utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

// 这行代码会生成 joke.g.dart 文件
part 'joke.g.dart';

/// 使用注解定义 FutureProvider
///
/// @riverpod 注解会自动生成 randomJokeProvider
/// 不需要手动创建 FutureProvider
@riverpod
Future<Joke> randomJoke(RandomJokeRef ref) async {
  // Fetching a random joke from a public API
  final data = await DioUtil.getData<Map<String, Object?>>(
    'https://official-joke-api.appspot.com/random_joke',
  );

  return Joke.fromJson(data);
}

class Joke {
  Joke({
    required this.type,
    required this.setup,
    required this.punchline,
    required this.id, 
  });

  factory Joke.fromJson(Map<String, Object?> json) {
    return Joke(
      type: json['type']! as String,
      setup: json['setup']! as String,
      punchline: json['punchline']! as String,
      id: json['id']! as int,
    );
  }

  final String type;
  final String setup;
  final String punchline;
  final int id;
}
